// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: hyuan

#pragma once

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/order/bybit.pb.h"
#include "coin2/exchange/bybit/symbology/product.h"
#include "coin2/exchange/bybit/api_util/auth_key.h"
#include "coin2/exchange/bybit/api_util/rate_tracker.h"

class BybitNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using RestParams = coin2::exchange::order::BybitSpotRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using ActionType = coin2::exchange::base::order::NativePrivateClient::ActionType;
  using QueryType = coin2::exchange::bybit::api_util::QueryType;
  using BybitRateTracker = coin2::exchange::bybit::api_util::BybitRateTracker;
  using BybitProduct = coin2::exchange::bybit::symbology::BybitProduct;

 public:
  explicit BybitNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

  std::shared_ptr<AsioHttpContext> QueryAccountBalance();
  std::shared_ptr<AsioHttpContext> QueryOpenOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryHistoryOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryFills(const RestParams& params);

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);
  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> SubmitOrderExecution(const OrderExecutionProto& order) override;
  std::shared_ptr<AsioHttpContext> SubmitRestQuery(const RestQueryProto& query) override;

  void Prepare();
  std::string DebugString() const;

 private:
  int64_t start_time_;
  int64_t enable_time_ {0};
  std::unordered_map<std::string, int32_t> api_block_info_;
  std::unordered_map<std::string, int32_t> api_req_info_;
  BybitRateTracker* local_rate_tracker_;
};
